﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Model;
using Cashier_Service;
using Cashier.utils;
using Cashier_Utils;
using Cashier_Interface;

namespace Cashier.member
{
    public partial class MemberRechargeAddForm : Form
    {
        private MainForm mainform;
        private IReloadData iReloadData;
        private MemberService memberService;
        private MemberRechargeService memberRechargeService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="iReloadData"></param>
        /// <param name="memberno"></param>
        public MemberRechargeAddForm(MainForm mainform, IReloadData iReloadData, String memberno)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.iReloadData = iReloadData;
            this.memberService = new MemberService();
            this.memberRechargeService = new MemberRechargeService();

            //设置权限
            Button[] buttons = { button_recharge };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);

            this.InitialFormData(memberno);
            textbox_memberno.LostFocus += new EventHandler(textbox_memberno_LostFocus);
        }

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        /// <param name="memberno"></param>
        private void InitialFormData(String memberno)
        {
            if (!String.IsNullOrEmpty(memberno))
            {
                Member member = memberService.GetMember(0, memberno);
                if (member != null)
                {
                    textbox_memberno.Text = member.Memberno;
                    label_name.Text = member.Name;
                    textbox_money.Text = "0.00";
                    textbox_giftmoney.Text = "0.00";
                    textbox_payMoney.Text = "0.00";
                    textbox_note.Text = String.Empty;
                }
            }
        }

        /// <summary>
        /// 会员编号失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_memberno_LostFocus(object sender, EventArgs e)
        {
            String memberno = textbox_memberno.Text.Trim();
            this.InitialFormData(memberno);
        }

        /// <summary>
        /// 保存充值信息
        /// </summary>
        private void SaveRecharge()
        {
            try
            {
                String memberno = textbox_memberno.Text.Trim();
                String memberName = label_name.Text;
                Decimal money = Decimal.Parse(textbox_money.Text.Trim());
                Decimal giftMoney = Decimal.Parse(textbox_giftmoney.Text.Trim());
                Decimal payMoney = Decimal.Parse(textbox_payMoney.Text.Trim());
                String note = textbox_note.Text.Trim();

                if (String.IsNullOrEmpty(memberno))
                {
                    MessageBox.Show("会员编号不能为空！");
                    return;
                }
                if (money <= 0)
                {
                    MessageBox.Show("充值金额必须大于0！");
                    return;
                }
                if (payMoney <= 0)
                {
                    MessageBox.Show("实付现金金额不得小于0！");
                    return;
                }
                Member member = memberService.GetMember(0, memberno);
                if (member == null || !member.Name.Equals(memberName))
                {
                    MessageBox.Show("会员编号有误！");
                    return;
                }

                Admin admin = this.mainform.Admin;
                MemberRecharge memberRecharge = new MemberRecharge();
                memberRecharge.EnterpriseNo = admin.EnterpriseNo;
                memberRecharge.StoreNo = admin.StoreNo;
                memberRecharge.TransNo = GuidUtil.GetGuidToLong().ToString();
                memberRecharge.MemberNo = memberno;
                memberRecharge.Money = money;
                memberRecharge.GiftMoney = giftMoney;
                memberRecharge.PayMoney = payMoney;
                memberRecharge.RechargeTime = DateTime.Now;
                memberRecharge.Note = note;
                memberRecharge.AdminUsername = admin.Username;
                memberRecharge.AdminName = admin.Name;
                memberRecharge.CreateTime = DateTime.Now;
                long id = memberRechargeService.InsertMemberRecharge(memberRecharge);
                if (id > 0)
                {
                    MessageBox.Show("充值成功！");
                    iReloadData.ReloadDataGridViewData(null);
                    this.ClearFormData();
                }
                else
                {
                    MessageBox.Show("充值失败！");
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 清理Form数据
        /// </summary>
        private void ClearFormData()
        {
            textbox_memberno.Text = String.Empty;
            textbox_money.Text = "0.00";
            textbox_giftmoney.Text = "0.00";
            textbox_payMoney.Text = "0.00";
            label_name.Text = String.Empty;
            textbox_note.Text = String.Empty;
        }

        /// <summary>
        /// 保存充值事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_recharge_Click(object sender, EventArgs e)
        {
            this.SaveRecharge();
        }

        /// <summary>
        /// 退出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(label_name.Text.Trim()))
            {
                DialogResult result = MessageBox.Show("确定要退出吗？", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }
    }
}
